package com.wxw.BaseLibrary.adapter;

import android.content.Context;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import com.wxw.BaseLibrary.component.SimpleTextItemView;
import com.wxw.BaseLibrary.component.SimpleTextItemWithShadowView;
import com.wxw.BaseLibrary.element.IListViewElement;

import java.util.List;

/**
 * Created by david on 2014-06-16.
 */
public class ElementListViewAdapter<T extends IListViewElement> extends BaseAdapter implements IElementListAdapter {
    private Context _context;
    private List<T> _listData;
    private int resourceID = -1;

    public ElementListViewAdapter(List<T> data){
        this._listData = data;
    }
    @Override
    public int getCount() {
        return _listData==null?0:_listData.size();
    }

    @Override
    public Object getItem(int i) {
        return _listData==null || i<0 || i>=_listData.size()?null:_listData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        if (_listData.size() <= i) {
            return null;
        }
        T t = _listData.get(i);
        if (t.getView() == null) {
            convertView = t.createView(this);
        }
        if (convertView == null) {
            convertView = t.getView();
        }
        convertView = t.setViewContent(convertView);
        return convertView;
    }

    @Override
    public Context getContext() {
        return _context;
    }

    @Override
    public void setContext(Context context) {
        _context = context;
    }

    @Override
    public int getViewResourceID() {
        return resourceID;
    }

    @Override
    public void setViewResourceID(int id) {
        resourceID = id;
    }
}
